package com.wanchuang.qhbb.module.mine;

/**
 * 文件名 : EarningDetailActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/28 20:06
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.wanchuang.qhbb.R;
import com.wanchuang.qhbb.base.BaseActivity;
import com.wanchuang.qhbb.net.Apis;
import com.wanchuang.qhbb.net.NetworkManager;
import com.wanchuang.qhbb.net.interfaces.OnRequestCallBack;
import com.wanchuang.qhbb.proto.MemberBank;
import com.wanchuang.qhbb.utils.StatusBarUtil;
import com.wanchuang.qhbb.utils.ValidateUtils;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class BankCardActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_card_number)
    TextView tvCardNumber;
    @BindView(R.id.btn_add_card)
    Button btnAddCard;
    @BindView(R.id.rv_bank)
    RelativeLayout rvBank;
    @BindView(R.id.ll_none)
    LinearLayout llNone;
    @BindView(R.id.tv_text_one)
    TextView tvTextOne;


    private String bank_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bank_card);
        StatusBarUtil.setWhiteStatusBar(this);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("我的银行卡");
        tvTextOne.setMovementMethod(new ScrollingMovementMethod());

    }


    // 加载检查是否有绑定银行卡
    private void isBindBankCard() {
        NetworkManager.INSTANCE.post(Apis.member_bank, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                MemberBank.get_bank bean = MemberBank.get_bank.parseFrom(response);
                List<MemberBank.get_bank.Describe> list = bean.getDesListList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getDes()).append("\n");
                }
                des.append(list.get(list.size() - 1).getDes());
                tvTextOne.setText(des.toString());
                List<MemberBank.bank> datas = bean.getArrayList();
                if (ValidateUtils.isValidate(datas)) {
                    rvBank.setVisibility(View.VISIBLE);
                    llNone.setVisibility(View.GONE);
                    String bank_card = datas.get(0).getBankCard();
                    String substring = bank_card.substring(bank_card.length() - 4);
                    tvBankName.setText(datas.get(0).getBankName());
                    tvCardNumber.setText("**** **** **** " + substring);
                    tvType.setText(datas.get(0).getBankType());
                    btnAddCard.setText("换绑");
                    bank_id = datas.get(0).getCardId();
                } else {
                    rvBank.setVisibility(View.GONE);
                    llNone.setVisibility(View.VISIBLE);
                    btnAddCard.setText("绑定银行卡");
                }

            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }


    @Override
    protected void onResume() {
        super.onResume();
        isBindBankCard();
    }

    @OnClick({R.id.back_iv, R.id.btn_add_card})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.btn_add_card:
                    startActivity(BindBankPayActivity.class);
                break;

        }
    }



}
